﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDMS.CommonForm
{
    public class TabManager
    {
        DevExpress.XtraTab.XtraTabControl tabControlName;
        public TabManager(DevExpress.XtraTab.XtraTabControl tabControlName)
        {
            this.tabControlName = tabControlName;
            this.tabControlName.CloseButtonClick += tabControlName_CloseButtonClick;
        }

        void tabControlName_CloseButtonClick(object sender, EventArgs e)
        {
            RemoveTab();
        }
        /// <summary>
        /// Check exists of TabPage
        /// </summary>
        /// <param name="tabControlName">XtraTabControl is parent</param>
        /// <param name="tabName">Name of Tab page</param>
        /// <param name="tabText">Text of Tab page</param>
        /// <returns></returns>
        public int TabPageExist(string tabName, string tabText)
        {
            int re = -1;
            for (int i = 0; i < tabControlName.TabPages.Count; i++)
            {
                if ((!string.IsNullOrEmpty(tabName) && tabControlName.TabPages[i].Name == tabName) || (!string.IsNullOrEmpty(tabName) && tabControlName.TabPages[i].Text == tabText))
                {
                    re = i;
                    break;
                }
            }
            return re;

            //re >= 0 is existed
        }

        /// <summary>
        /// Add a XtraTabPage to XtraTabControl
        /// </summary>
        /// <param name="tabControlName"></param>
        /// <param name="tabName">Name of Tab page</param>
        /// <param name="tabText">Text of Tab page</param>
        /// <param name="form">XtraForm</param>
        /// <param name="imageIndex"></param>
        public void AddTab(string tabName, string tabText, DevExpress.XtraEditors.XtraForm form, int imageIndex)
        {
            int index = TabPageExist(tabName, tabText);
            if (index >= 0)
            {
                tabControlName.SelectedTabPage = tabControlName.TabPages[index];
                if (!string.IsNullOrEmpty(tabName))
                    tabControlName.SelectedTabPage.Name = tabName;
                else if (!string.IsNullOrEmpty(tabText))
                    tabControlName.SelectedTabPage.Text = tabText;
            }
            else
            {
                DevExpress.XtraTab.XtraTabPage tabpage = new DevExpress.XtraTab.XtraTabPage
                {
                    Text = tabText,
                    Name = tabName,
                    ImageIndex = imageIndex
                };
                tabControlName.TabPages.Add(tabpage);
                tabControlName.SelectedTabPage = tabpage;

                form.TopLevel = false;
                form.Parent = tabpage;
                form.FormBorderStyle = FormBorderStyle.None;
                form.Show();
                form.Dock = DockStyle.Fill;
            }
        }
        public void AddTab(string tabName, string tabText, System.Windows.Forms.UserControl userControl, int imageIndex)
        {
            int index = TabPageExist(tabName, tabText);
            if (index >= 0)
            {
                tabControlName.SelectedTabPage = tabControlName.TabPages[index];
                if (!string.IsNullOrEmpty(tabName))
                    tabControlName.SelectedTabPage.Name = tabName;
                else if (!string.IsNullOrEmpty(tabText))
                    tabControlName.SelectedTabPage.Text = tabText;
            }
            else
            {
                DevExpress.XtraTab.XtraTabPage tabpage = new DevExpress.XtraTab.XtraTabPage
                {
                    Text = tabText,
                    Name = tabName,
                    ImageIndex = imageIndex
                    
                };
                tabpage.Controls.Add(userControl);
                userControl.Dock = DockStyle.Fill;

                tabControlName.TabPages.Add(tabpage);
                tabControlName.SelectedTabPage = tabpage;
                
            }
        }
        public void RemoveTab()
        {
            tabControlName.TabPages.RemoveAt(tabControlName.SelectedTabPageIndex);
            tabControlName.SelectedTabPageIndex = tabControlName.TabPages.Count - 1;
        }
    }
}
